import 'dart:io';

import 'package:flutter/material.dart';

class AppSizeMonitorPage extends StatefulWidget {
  AppSizeMonitorPage({
    Key? key,
  }) : super(key: key);

  @override
  _AppSizeMonitorPageState createState() => _AppSizeMonitorPageState();
}

class _AppSizeMonitorPageState extends State<AppSizeMonitorPage> {
  late AppSizeTool appSizeTool;
  late Future<List<FileNode>> fileNodes;

  @override
  void initState() {
    super.initState();
    appSizeTool = AppSizeTool();
    try {
      final files = appSizeTool.getFileNodes('Users/qxq4633/Desktop/apk/mymw.app');
      fileNodes = Future.value(files);
    } on Object catch (error) {
      fileNodes = Future.value([]);
      print('unable to load the apk or ipa ${error.toString()}');
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<FileNode>>(
        future: fileNodes,
        builder: (context, snapShot) {
          switch (snapShot.connectionState) {
            case ConnectionState.done:
              return Material(
                child: ListView(
                  children: [
                    ListTile(
                      title: Text('title'),
                    ),
                  ],
                ),
              );
            default:
              return CircularProgressIndicator();
          }
        });
  }
}

class FileNode {
  final bool isDirectory;
  final String name;
  final FileStat stat;
  List<FileNode> children = [];

  FileNode({
    required this.isDirectory,
    required this.name,
    required this.stat,
    required List<FileNode> children,
  });
}

class AppSizeTool {
  List<FileNode> getFileNodes(String path) {
    List<FileNode> children = [];

    if (FileSystemEntity.isDirectorySync(path)) {
      final directory = Directory(path);
      List<FileNode> directoryFileNodes = [];
      List<FileNode> subNodes = [];
      final fileList = directory.listSync(recursive: false, followLinks: false);
      if (fileList.isNotEmpty) {
        for (var index = 0; index < fileList.length; index++) {
          final fileEnty = fileList[index];
          subNodes = getFileNodes(fileList[index].path);
          directoryFileNodes.add(FileNode(
            isDirectory: FileSystemEntity.isDirectorySync(fileEnty.path),
            name: fileEnty.path.split('/').last,
            stat: fileEnty.statSync(), //stats
            children: subNodes,
          ));
        }
      }

      // if (!path.endsWith('Frameoworks') ||
      //     !path.endsWith('bundle') ||
      //     !path.endsWith('dylib')) {

      //   }
      children.add(FileNode(
        isDirectory: true,
        name: path.split('/').last,
        stat: directory.statSync(),
        children: directoryFileNodes,
      ));
    } else {
      children.add(FileNode(
        isDirectory: false,
        name: path.split('/').last,
        stat: File(path).statSync(),
        children: [],
      ));
    }

    return children;
  }
}
